package com.fausto.learnimprint.config

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration
import org.springframework.core.env.Environment
import org.springframework.core.env.Profiles
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
class WebMvcConfig : WebMvcConfigurer {

    @Autowired
    lateinit var env: Environment

    /**
     *      资源映射配置
     */
    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        //若不是开发环境,swagger未开启则直接返回
        if (!env.acceptsProfiles(Profiles.of("dev"))) return
        //将swagger-ui.html 添加 到 resources目录下
        registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/")
        registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/")
        registry.addResourceHandler("/web_frontend/**")
            .addResourceLocations("classpath:/web_frontend/")
    }

}